package com.mazegame.frontend;

import com.mazegame.backend.BackgroundMusic;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.AudioClip;

import javax.swing.*;
import java.awt.*;
import java.net.URI;
import java.net.URISyntaxException;

// 定义一个接口来统一管理不同的游戏界面
interface GamePanel {
    void showPanel();
    void cleanup();
}

// 界面工厂类，用于创建不同的游戏界面
class PanelFactory {
    public static GamePanel createPanel(String panelType, CardLayout cardLayout, JPanel cardPanel) {
        if ("STARTPANEL".equals(panelType)) {
            return new StartPanel(cardLayout, cardPanel);
        }
        if("LOGINPANEL".equals(panelType)){
            return new LoginPanel(cardLayout, cardPanel);
        }
        if("REGISTERPANEL".equals(panelType)){
            return new RegisterPanel(cardLayout, cardPanel);
        }
        if("CHARACTERPANEL".equals(panelType)){
            return new CharacterPanel(cardLayout, cardPanel);
        }
        if("OPTIONPANEL".equals(panelType)){
            return new OptionPanel(cardLayout, cardPanel);
        }
        if("MAINPANEL".equals(panelType)){
            return new MainPanel(cardLayout, cardPanel);
        }
        if("CHAPTERPANEL".equals(panelType)){
            return new ChapterPanel(cardLayout, cardPanel);
        }
        if("FIRSTSTORYPANEL".equals(panelType)){
            return new FirstStoryPanel(cardLayout, cardPanel);
        }
        if("SECONDSTORYPANEL".equals(panelType)){
            return new SecondStoryPanel(cardLayout, cardPanel);
        }
        if("THIRDSTORYPANEL".equals(panelType)){
            return new ThirdStoryPanel(cardLayout, cardPanel);
        }
        if("MAZEGENERATEPANEL".equals(panelType)){
            return new MazeGeneratePanel(cardLayout, cardPanel);
        }
        if("BUFFERPANEL".equals(panelType)){
            return new BufferPanel(cardLayout, cardPanel);
        }
        if("MAZEGENERATEPANEL2".equals(panelType)){
            return new MazeGeneratePanel2(cardLayout, cardPanel);
        }
        if("ENDPANEL".equals(panelType)){
            return new EndPanel(cardLayout, cardPanel);
        }
        if("RANKPANEL".equals(panelType)){
            return new RankPanel(cardLayout, cardPanel);
        }
        // 可以在这里添加更多的界面类型
        return null;
    }
}

public class MainFrame {
    private JFrame frame;// Java swing中用于创建和管理顶级窗口（即应用程序的主窗口）。
    public static CardLayout cardLayout;// 管理器，切换卡片面板
    public static JPanel cardPanel;// 容器，保存卡片面板
    private static final int WIDTH = 1500;  // 统一宽度
    private static final int HEIGHT = 930; // 统一高度
    private GamePanel startPanel; // 保存StartPanel实例
    private GamePanel mainPanel;// 保存MainPanel实例

    private AudioClip backgroundMusic;
    private void initializeMusic() {
        try {
            // 与你的音效加载方式保持一致，使用getResource获取资源
            URI musicUri = getClass().getResource("/music/backgroundmusic.wav").toURI();
            backgroundMusic = new AudioClip(musicUri.toString());
            backgroundMusic.setVolume(0.5); // 设置音量（0.0~1.0）
            backgroundMusic.setCycleCount(AudioClip.INDEFINITE); // 无限循环
        } catch (URISyntaxException e) {
            System.err.println("背景音乐路径错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    // 播放背景音乐
    public void playBackgroundMusic() {
        if (backgroundMusic != null) {
            backgroundMusic.play();
        }
    }

    // 停止背景音乐
    public void stopBackgroundMusic() {
        if (backgroundMusic != null) {
            backgroundMusic.stop();
        }
    }


    public MainFrame() {
        initializeJavaFX();
        initializeFrame();
        addPanelsToCardLayout();
        //BackgroundMusic.playMusic("src/main/resources/music/backgroundmusic.wav");

        startPanel.showPanel();
        //cardLayout.show(cardPanel, "MAINPANEL");
    }


    private void initializeJavaFX() {
        new JFXPanel();// JFXPanel 是 JavaFX 提供的一个 Swing 组件（容器），它允许你在 Swing 应用中嵌入 JavaFX 的内容（如 Scene、WebView、视频播放器等）。
    }

    private void initializeFrame() {
        initializeMusic(); // 初始化音乐
        playBackgroundMusic(); // 启动音乐

        frame = new JFrame("迷宫闯关游戏");// 创建JFrame（主窗口）
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗口时退出程序
        frame.setSize(WIDTH, HEIGHT);// 设置窗口大小
        frame.setLocationRelativeTo(null);// 设置窗口居中显示
        frame.setResizable(false);// 设置窗口不可变

        cardLayout = new CardLayout();// 创建CardLayout（卡片面板管理器）
        cardPanel = new JPanel(cardLayout);//  创建JPanel（卡片面板容器），并设置卡片面板管理器
        frame.setContentPane(cardPanel);// 将卡片面板容器设置为主窗口的内容面板
    }



    public void addPanelsToCardLayout() {
        // 使用工厂模式创建StartPanel并保存实例
        startPanel = PanelFactory.createPanel("STARTPANEL", cardLayout, cardPanel);// 创建开始面板并保存实例
        GamePanel loginPanel = PanelFactory.createPanel("LOGINPANEL", cardLayout, cardPanel);// 创建登录面板
        GamePanel registerPanel = PanelFactory.createPanel("REGISTERPANEL", cardLayout, cardPanel);// 创建注册面板
        GamePanel characterPanel = PanelFactory.createPanel("CHARACTERPANEL", cardLayout, cardPanel);// 创建角色面板
        GamePanel optionPanel = PanelFactory.createPanel("OPTIONPANEL", cardLayout, cardPanel);// 创建选择面板
        mainPanel = PanelFactory.createPanel("MAINPANEL", cardLayout, cardPanel);// 创建游戏主界面面板
        GamePanel chapterPanel = PanelFactory.createPanel("CHAPTERPANEL", cardLayout, cardPanel);// 创建章节面板
        GamePanel firstStoryPanel = PanelFactory.createPanel("FIRSTSTORYPANEL", cardLayout, cardPanel);// 创建故事面板
        GamePanel secondStoryPanel = PanelFactory.createPanel("SECONDSTORYPANEL", cardLayout, cardPanel);
        GamePanel thirdStoryPanel = PanelFactory.createPanel("THIRDSTORYPANEL", cardLayout, cardPanel);
        GamePanel mazeGeneratePanel = PanelFactory.createPanel("MAZEGENERATEPANEL", cardLayout, cardPanel);
        GamePanel bufferPanel = PanelFactory.createPanel("BUFFERPANEL", cardLayout, cardPanel);
        GamePanel mazeGeneratePanel2 = PanelFactory.createPanel("MAZEGENERATEPANEL2", cardLayout, cardPanel);
        GamePanel endPanel = PanelFactory.createPanel("ENDPANEL", cardLayout, cardPanel);
        GamePanel rankPanel = PanelFactory.createPanel("RANKPANEL", cardLayout, cardPanel);


        cardPanel.add((JPanel) registerPanel, "REGISTERPANEL");// 将注册面板添加到卡片面板中
        cardPanel.add((JPanel) startPanel, "STARTPANEL");// 将开始面板添加到卡片面板中
        cardPanel.add((JPanel) loginPanel, "LOGINPANEL");// 将登录面板添加到卡片面板中
        cardPanel.add((JPanel) characterPanel, "CHARACTERPANEL");// 将角色面板添加到卡片面板中
        cardPanel.add((JPanel) optionPanel, "OPTIONPANEL");// 将选择面板添加到卡片面板中
        cardPanel.add((JPanel) mainPanel, "MAINPANEL");// 将主界面面板添加到卡片面板中
        cardPanel.add((JPanel) chapterPanel, "CHAPTERPANEL");// 将章节面板添加到卡片面板中
        cardPanel.add((JPanel) firstStoryPanel, "FIRSTSTORYPANEL");// 将故事面板添加到卡片面板中
        cardPanel.add((JPanel) secondStoryPanel, "SECONDSTORYPANEL");
        cardPanel.add((JPanel) thirdStoryPanel, "THIRDSTORYPANEL");
        cardPanel.add((JPanel) mazeGeneratePanel, "MAZEGENERATEPANEL");
        cardPanel.add((JPanel) bufferPanel, "BUFFERPANEL");
        cardPanel.add((JPanel) mazeGeneratePanel2, "MAZEGENERATEPANEL2");
        cardPanel.add((JPanel) endPanel, "ENDPANEL");
        cardPanel.add((JPanel) rankPanel, "RANKPANEL");
    }

    public void show() {
        frame.setVisible(true);// 显示主窗口
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new MainFrame().show();
        });
    }
}